package com.bjsxt.controller.doctor;

import cn.hutool.core.bean.BeanUtil;
import com.bjsxt.controller.BaseController;
import com.bjsxt.domain.*;
import com.bjsxt.dto.PatientDto;
import com.bjsxt.service.CareHistoryService;
import com.bjsxt.service.PatientService;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 患者档案控制层
 */
@RestController
@RequestMapping("doctor/patient")
public class PatientController extends BaseController {

    @Reference
    private PatientService patientService;

    @Reference
    private CareHistoryService careHistoryService;

    /**
     * 分页查询患者信息
     *
     * @param patientDto
     * @return
     */
    @GetMapping("listPatientForPage")
    @HystrixCommand
    public AjaxResult listPatientForPage(PatientDto patientDto) {
        DataGridView dataGridView = this.patientService.listPatientForPage(patientDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 根据患者id查询患者信息
     *
     * @param patientId
     * @return
     */
    @GetMapping("getPatientById/{patientId}")
    @HystrixCommand
    public AjaxResult getPatientById(@Validated @PathVariable String patientId) {
        Patient patient = this.patientService.getPatientById(patientId);
        return AjaxResult.success(patient);
    }

    /**
     * 根据患者id查询患者档案
     *
     * @param patientId
     * @return
     */
    @GetMapping("getPatientFileById/{patientId}")
    @HystrixCommand
    public AjaxResult getPatientFileById(@Validated @PathVariable String patientId) {
        PatientFile patientFile = this.patientService.getPatientFileById(patientId);
        return AjaxResult.success(patientFile);
    }

    /**
     * 根据患者id查询患者信息 患者档案信息 历史病例
     *
     * @param patientId
     * @return
     */
    @GetMapping("getPatientAllMessageByPatientId/{patientId}")
    @HystrixCommand
    public AjaxResult getPatientAllMessageByPatientId(@Validated @PathVariable String patientId) {
//        根据患者id查询病例信息
        List<CareHistory> careHistories = this.careHistoryService.queryCareHistoryByPatientId(patientId);
        List<Map<String, Object>> res = new ArrayList<>();
        for (CareHistory careHistory : careHistories) {
            Map<String, Object> careHistoryMap = BeanUtil.beanToMap(careHistory);
            careHistoryMap.put("careOrders", Collections.EMPTY_LIST);
            List<Map<String, Object>> reCareOrders = new ArrayList<>();
//            根据病例id查询处方列表
            List<CareOrder> careOrders = this.careHistoryService.queryCareOrdersByChId(careHistory.getChId());
            for (CareOrder careOrder : careOrders) {
                Map<String, Object> careOrderMap = BeanUtil.beanToMap(careOrder);
                List<CareOrderItem> careOrderItems = this.careHistoryService.queryCareOrderItemsByCoId(careOrder.getCoId(), null);
                careOrderMap.put("careOrderItems", careOrderItems);
                reCareOrders.add(careOrderMap);
            }
            careHistoryMap.put("careOrders", reCareOrders);
            res.add(careHistoryMap);
        }
        return AjaxResult.success(res);
    }
}
